package com.leyou.item.web;


import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {


    private final BrandService brandService;

    public BrandController(BrandService brandService) {
        this.brandService = brandService;
    }


    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/id")
    public ResponseEntity<BrandDTO> findBrandById(@PathVariable("id") Long id){
        return ResponseEntity.ok(this.brandService.findBrandById(id));
    }

    /**
     * 品牌管理
     * @param page
     * @param rows
     * @param key
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> pageQuery(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows ,
            @RequestParam(value = "key",required = false) String key

    ){
        return ResponseEntity.ok(this.brandService.pageQuery(page,rows,key));
    }

    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> listBrandByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(this.brandService.listBrandByIds(ids));
    }

    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> listBrandByCategory(@RequestParam("id") Long cid){
        return ResponseEntity.ok(this.brandService.listBrandByCategory(cid));
    }

    //列出所有属性直接接收，也可直接用对象接受

    /*@PostMapping
    public ResponseEntity<Void> addBrand(
            @RequestParam ("name") String name,
            @RequestParam (value = "image" ,required = false) String image,
            @RequestParam ("categoryIds") Long categoryIds,
            @RequestParam ("letter") Character letter
            ){

    }*/

    @PostMapping
    public ResponseEntity<Void> addBrand(
            BrandDTO brandDTO
    ){
        this.brandService.addBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> updateBrand(
           BrandDTO brandDTO
    ){
        this.brandService.updateBrand(brandDTO);
       return ResponseEntity.ok().build();
    }

}
